import BaseView from "../base/BaseView";
import KLineTrendComponent from "../base/components/KLineTrendComponent";
import TushareModule from "../module/TushareModule";
import DropList from "./items/DropList";

const {ccclass, property} = cc._decorator;

@ccclass
export default class StockKLineView extends BaseView {

    @property(DropList)
    dropName: DropList = null;

    @property(cc.Prefab)
    klineTrendPrefab: cc.Prefab = null;

    @property(cc.Node)
    contents: cc.Node[] = [];
    
    klineTrends: KLineTrendComponent[] = [];
    
    _stocks: any = [];
    
    _selectIndex: number = 0;

    _waitFlag: boolean = false;
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        for (let index = 0; index < this.contents.length; index++) {
            const content = this.contents[index];
            let node = cc.instantiate(this.klineTrendPrefab);
            node.height = content.height;
            node.width = content.width;
            content.addChild(node);
            this.klineTrends[index] = node.getComponent(KLineTrendComponent);
        }
    }   

    show (params) {
        super.show(params);
        this._stocks = params.stocks;
        this._selectIndex = params.selectIndedx;
        let names = [];
        for (let index = 0; index < this._stocks.length; index++) {
            const item = this._stocks[index];
            names.push(item.name);
        }
        this.dropName.setData(names, this._selectIndex);
        this.updateView();
        
    }

    async updateView () {
        let item = this._stocks[this._selectIndex];
        let ts_code = item.ts_code;
        let stockKline: any = await TushareModule.getInstance().getStockKLine(ts_code);
        let daily = stockKline.dailyList;
        let week = stockKline.weekList;
        let month = stockKline.monthList;
        this.klineTrends[0].setData({list: daily.slice(daily.length - 220, daily.length)}, 10, 0.5, '_qfq');
        this.klineTrends[1].setData({list: week.slice(week.length - 110, week.length)}, 10, 0.5, '_qfq');
        this.klineTrends[2].setData({list: month.slice(month.length - 110, month.length)}, 10, 0.5, '_qfq');
    }

    // update (dt) {}
}
